package com.david.bluetooth.ble.musicctr

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.RecyclerView
import com.david.bluetooth.R

class MusicClientActivity: AppCompatActivity() {
    private var actionLog: TextView? = null
    private var connectStatus: TextView? = null

    private lateinit var deviceList: RecyclerView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_musicble_client)


        findViewById<View>(R.id.music_start).setOnClickListener {
            checkAdapter()

            //write(ACTION_TYPE_MUSIC, MUSIC_ACTION_PLAY)
            val intent = Intent(this, BleClientMusicService::class.java)
            intent.putExtra(BleClientMusicService.BLE_MUSIC_CLIENT_CMD_KEY, ACTION_TYPE_MUSIC)
            intent.putExtra(BleClientMusicService.BLE_MUSIC_CLIENT_CMD_ACTION_KEY, MUSIC_ACTION_PLAY)
            //intent.putExtra(BleClientMusicService.BLE_MUSIC_CLIENT_ACTION_DATA_KEY, )
            startForegroundService(intent)
        }
        findViewById<View>(R.id.music_stop).setOnClickListener {
            checkAdapter()

            //write(ACTION_TYPE_MUSIC, MUSIC_ACTION_PAUSE)
            val intent = Intent(this, BleClientMusicService::class.java)
            intent.putExtra(BleClientMusicService.BLE_MUSIC_CLIENT_CMD_KEY, ACTION_TYPE_MUSIC)
            intent.putExtra(BleClientMusicService.BLE_MUSIC_CLIENT_CMD_ACTION_KEY, MUSIC_ACTION_PAUSE)
            //intent.putExtra(BleClientMusicService.BLE_MUSIC_CLIENT_ACTION_DATA_KEY, )
            startForegroundService(intent)
        }
        findViewById<View>(R.id.music_previous).setOnClickListener {
            checkAdapter()

            //write(ACTION_TYPE_MUSIC, MUSIC_ACTION_PREVIOUS)
            val intent = Intent(this, BleClientMusicService::class.java)
            intent.putExtra(BleClientMusicService.BLE_MUSIC_CLIENT_CMD_KEY, ACTION_TYPE_MUSIC)
            intent.putExtra(BleClientMusicService.BLE_MUSIC_CLIENT_CMD_ACTION_KEY, MUSIC_ACTION_PREVIOUS)
            //intent.putExtra(BleClientMusicService.BLE_MUSIC_CLIENT_ACTION_DATA_KEY, )
            startForegroundService(intent)
        }
        findViewById<View>(R.id.music_next).setOnClickListener {
            checkAdapter()

            //write(ACTION_TYPE_MUSIC, MUSIC_ACTION_NEXT)
            val intent = Intent(this, BleClientMusicService::class.java)
            intent.putExtra(BleClientMusicService.BLE_MUSIC_CLIENT_CMD_KEY, ACTION_TYPE_MUSIC)
            intent.putExtra(BleClientMusicService.BLE_MUSIC_CLIENT_CMD_ACTION_KEY, MUSIC_ACTION_NEXT)
            //intent.putExtra(BleClientMusicService.BLE_MUSIC_CLIENT_ACTION_DATA_KEY, )
            startForegroundService(intent)
        }

        findViewById<View>(R.id.music_volume_down).setOnClickListener {
            checkAdapter()

            //write(ACTION_TYPE_SYSTEM, SYSTEM_ACTION_VOLUME_DOWN)
            //write(ACTION_TYPE_MUSIC, MUSIC_ACTION_NEXT)
            val intent = Intent(this, BleClientMusicService::class.java)
            intent.putExtra(BleClientMusicService.BLE_MUSIC_CLIENT_CMD_KEY, ACTION_TYPE_SYSTEM)
            intent.putExtra(BleClientMusicService.BLE_MUSIC_CLIENT_CMD_ACTION_KEY, SYSTEM_ACTION_VOLUME_DOWN)
            //intent.putExtra(BleClientMusicService.BLE_MUSIC_CLIENT_ACTION_DATA_KEY, )
            startForegroundService(intent)
        }

        findViewById<View>(R.id.music_volume_up).setOnClickListener {
            checkAdapter()

            //write(ACTION_TYPE_SYSTEM, SYSTEM_ACTION_VOLUME_UP)
            val intent = Intent(this, BleClientMusicService::class.java)
            intent.putExtra(BleClientMusicService.BLE_MUSIC_CLIENT_CMD_KEY, ACTION_TYPE_SYSTEM)
            intent.putExtra(BleClientMusicService.BLE_MUSIC_CLIENT_CMD_ACTION_KEY, SYSTEM_ACTION_VOLUME_UP)
            //intent.putExtra(BleClientMusicService.BLE_MUSIC_CLIENT_ACTION_DATA_KEY, )
            startForegroundService(intent)
        }

        findViewById<View>(R.id.music_ble_scan).setOnClickListener {
            checkAdapter()

            //reScan()
            val intent = Intent(this, BleClientMusicService::class.java)
            intent.putExtra(BleClientMusicService.BLE_MUSIC_CLIENT_CMD_KEY, BleClientMusicService.BLE_MUSIC_CLIENT_ACTION_CMD_SCAN)
            //intent.putExtra(BleClientMusicService.BLE_MUSIC_CLIENT_ACTION_DATA_KEY, )
            startForegroundService(intent)
        }

        actionLog = findViewById(R.id.tv_tips)
        connectStatus = findViewById(R.id.ble_connect_status)



        deviceList = findViewById<RecyclerView>(R.id.music_ble_server_list)
        deviceList?.let {
            it.adapter = BleClientMusicService.mBleDevAdapter
        }

        findViewById<TextView>(R.id.ble_connect_status).text = if (BleClientMusicService.isConnected) "已连接" else "未连接"
        findViewById<TextView>(R.id.tv_tips).text = BleClientMusicService.tips

        BleClientMusicService.onTipsChangeListener = object : OnTipsChangeListener{
            override fun onConnectChange(musicBleConnect: Boolean) {
                runOnUiThread {
                    findViewById<TextView>(R.id.ble_connect_status).text = if (musicBleConnect) "已连接" else "未连接"
                }
            }

            override fun onTipsChange(tips: String) {
                runOnUiThread {
                    findViewById<TextView>(R.id.tv_tips).text = tips
                }
            }

        }
    }

    private fun checkAdapter () {
        if (deviceList.adapter == null) {
            deviceList.adapter = BleClientMusicService.mBleDevAdapter
        }
    }
}